package com.aaa.service.system.impl;

import com.aaa.aspect.InsertAnn;
import com.aaa.aspect.UpdateAnn;
import com.aaa.dao.system.UserInfoDao;
import com.aaa.entity.PageSortEntity;
import com.aaa.entity.system.Userinfo;
import com.aaa.service.system.UserinfoService;
import com.aaa.utils.MyTools;
import com.aliyuncs.ram.model.v20150501.CreateUserResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author ：ludi（206913808@qq.com）
 * @date ：Created in 2022/8/29 16:45
 * @description：
 * @modified By：
 * @version:
 */
@Log
@Service
public class UserinfoServiceImpl implements UserinfoService {
    @Autowired
    UserInfoDao userInfoDao;

    /**
     * 查询所有
     *
     * @param pageSortEntity
     * @param userinfo
     * @return
     */
    @Override
    public Page queryAllUserinfoPage(PageSortEntity pageSortEntity, Userinfo userinfo) {
        PageHelper.startPage(pageSortEntity.getPage(), pageSortEntity.getLimit(), pageSortEntity.getOrderString());
        List<Map<String, Object>> mapList = userInfoDao.queryAllUserinfoPage(userinfo);
        Page page = (Page) mapList;
        return page;
    }

    /**
     * 根据名字查
     *
     * @param username
     * @return
     */
    @Override
    public Userinfo queryUserEntityByName(String username) {
        return userInfoDao.queryUserEntityByName(username);
    }

    /**
     * @param
     * @return
     * @create by: 张恒
     * @description: 添加
     * @create time: 2022/9/1 16:11
     */
    @InsertAnn
    @Override
    public boolean saveUserinfo(Userinfo userinfo) {
        String password = userinfo.getPassword();
        //密码加密,并返回,盐值和密文密码
        Map<String, String> stringMap = MyTools.makePasswordSalt(password);
        userinfo.setSalt(stringMap.get("salt"));
        //替换原有的明文密码
        userinfo.setPassword(stringMap.get("password"));
        return userInfoDao.saveUserinfo(userinfo);
    }

    /**
     * @param id
     * @return
     * @create by: 张恒
     * @description: 删除
     * @create time: 2022/9/1 18:55
     */
    @Override
    public boolean deleteUserinfoById(Integer[] id) {
        return userInfoDao.deleteUserinfoById(id);
    }


    /**
     * @param userinfo
     * @return
     * @create by: 张恒
     * @description: 修改
     * @create time: 2022/9/1 18:55
     */
    @UpdateAnn
    @Override
    public boolean updateUserinfo(Userinfo userinfo) {
        return userInfoDao.updateUserinfo(userinfo);
    }


    /**
     * @create by: 张恒
     * @description: 修改密码
     * @create time: 2022/9/13 10:59
     * @param userinfo
     * @return
     */
    @Override
    public boolean updatePasswordUserinfo(Userinfo userinfo) {
        String password = userinfo.getPassword();
        //密码加密,并返回,盐值和密文密码
        Map<String, String> stringMap = MyTools.makePasswordSalt(password);
        log.info(stringMap.get("salt"));
        userinfo.setSalt(stringMap.get("salt"));
        //替换原有的明文密码
        userinfo.setPassword(stringMap.get("password"));
        return userInfoDao.updateUserinfo(userinfo);
    }


    /**
     * @param
     * @return
     * @create by: 张恒
     * @description: 唯一校验
     * @create time: 2022/9/7 9:53
     */
    @Override
    public boolean repetitionName(Userinfo userinfo) {
        Integer integer = userInfoDao.repetitionName(userinfo);
        if (integer > 0) {
            return false;
        }
        return true;
    }
}